import {
  getSongDetail,
  getSongLyric,
  getSongComments,
  getSimiPlaylist,
  getSimiSong
} from '@/services/song-api'
import * as actionTypes from './constants'
import { formatLyric } from '@/utils/format'
const changeCurrentSongDetailAction = currentSongDetail => ({
  type: actionTypes.CHANGE_CURRENT_SONG_DETAIL_ACTION,
  currentSongDetail
})
const changeSongLyricAction = songLyric => ({
  type: actionTypes.CHANGE_SONG_LYRIC_ACTION,
  songLyric
})
const changeSongCommentsAction = commentList => ({
  type: actionTypes.CHANGE_SONG_COMMENTS_ACTION,
  commentList
})
const changeSongCommentTotalAction = totalComment => ({
  type: actionTypes.CHANGE_SONG_COMMENTS_TOTAL_ACTION,
  totalComment
})
const changeSimiPlaylistAction = simiPlaylists => ({
  type: actionTypes.CHANGE_SONG_SIMI_PLAYLIST_ACTION,
  simiPlaylists
})
const changeSimiSongAction = simiSongs => ({
  type: actionTypes.CAHNGE_SONG_SIMI_SONG_ACTION,
  simiSongs
})
export const getSongDetailAction = ids => {
  return async dispatch => {
    console.log(1111);
    const res = await getSongDetail(ids)
    const song = res.songs[0]
    const lyr_res = await getSongLyric(ids)
    const playlist_res = await getSimiPlaylist(ids)
    dispatch(changeSimiPlaylistAction(playlist_res.playlists))
    const song_res = await getSimiSong(ids)
    dispatch(changeSimiSongAction(song_res.songs))
    dispatch(changeCurrentSongDetailAction(song))
    dispatch(changeSongLyricAction(formatLyric(lyr_res.lrc.lyric))) 
    }
}
export const getSongCommentsAction = (id,pageSize,pageNo,sortType) => {
  return async dispatch => {
    const res = await getSongComments(id, pageSize, pageNo, sortType)
    
    dispatch(changeSongCommentsAction(res.comments))
    dispatch(changeSongCommentTotalAction(res.total))
  }
}
